package com.terrynoya.fight.commands
{
	public class CircularBuffer
	{
		private var _data:Array;
		private var _capacity:int;
		
		private var _readPos:int;
		private var _writePos:int;
		private var _wraparound:Boolean;
		private var _size:int;
		
		public function CircularBuffer(capacity:int)
		{
			this._capacity = capacity;
			this._data = new Array();
			this.clear();
		}
		
		private function clear():void
		{
			this._size = 0;
			this._writePos = 0;
			this._readPos = 0;
			this._wraparound = false;
		}
		
		public function get capacity():int
		{
			return _capacity;
		}

		public function get size():int
		{
			return _size;
		}

		public function add(obj:Object):void
		{
			this._data[this._writePos] = obj;
			this._writePos++;
			if(this._wraparound == false)
			{
				this._size ++;
			}
			else
			{
				this._readPos ++;
				if(this._readPos == this._capacity)
				{
					this._readPos = 0;
				}
			}
			
			if(this._writePos == this._capacity)
			{
				this._writePos = 0;
				this._wraparound = true;
			}
		}
		
		public function get(index:int):Object
		{
			var val:int = (index + this._readPos) % this._capacity;
			return this._data[val];
		}
		
		public function reverseGet(index:int):Object
		{
			var val:int = (this._size - index - 1 + this._readPos) % this._capacity;
			return this._data[val];
		}
		
		public function getText():String
		{
			var rlt:String = "";
			for (var i:int = 0; i < this._data.length; i++) 
			{
				rlt += this.get(i)+",";
				if(i == 60)
				{
					rlt += "\r\n";
				}
			}
			return rlt;
		}
	}
}